
var httpUtil = require("HttpUtil");
cc.Class({
    extends: cc.Component,

    properties: {
        questionTitle: {
            default: null,
            type: cc.Label
        },
       answerListNode: {
            default: null,
            type: cc.Node
       },
       restartBtn: {
           default: null,
           type: cc.Button
       },
       answerListNodeDefaultHeight: 0
    },
    onLoad: function () {
        //首个问题 
        /* var questionTitleStr = '1.请问你是男生还是女生，请回答请回答请回答请回答请回答？';
        var answerListData = 'A.男生,B.女生';
        var answerStrArray = answerListData.split(',');
        this.showQAContent(questionTitleStr,answerStrArray); */
        this.loadQAContent(1);

        this.answerListNodeDefaultHeight = this.answerListNode.height;
    },
    start () {

    },
    update (dt) {

    },
    //加载问题、选项 内容
    loadQAContent: function(questionId){
        var self = this;
        //过渡动画
        self.hideQA();
        this.answerListNode.height = this.answerListNodeDefaultHeight;
        this.answerListNode.removeAllChildren();


        //根据questionId网络加载问题
        httpUtil.request({
            url : "http://localhost:8081/?questionId=" + questionId,
            success : function(data){
                cc.log(data);
                //调用显示方法
                var questionTitleStr = data.questionTitle;
                var answerList = data.answerList;

                self.showQAContent(questionTitleStr,answerList);
                self.showQA();
            }
        });
    },
    //显示问题，选项
    showQAContent: function(questionTitleStr,answerList){

        var self = this;

        //设置界面内容
        this.questionTitle.string = questionTitleStr;
        
        for(var i = 0 ; i < answerList.length ; i++){ // 分割字符串 ， 遍历
            //新建一个Label对象
            var newAnswer = new cc.Node("answer" + i).addComponent(cc.Label);
            //把字符串赋值给label，string属性 能显示到界面上
            newAnswer.string = answerList[i].content;
            //this.answerListNode.addChild(newAnswer.node);
            //设置新Label的父节点 控制显示位置
            newAnswer.node.parent = this.answerListNode;
            //定义一个Label的自定义属性
            newAnswer["answerData"] = answerList[i];

            //var currentAnswerWidth = newAnswer.node.width;
            //得到新Label节点的高度（为了改变父节点的高度）
            var currentAnswerHeight = newAnswer.node.height;
            //设置position 位置。因为答案是竖排，所以 x坐标都是 0 ，而 y 坐标是计算出来的
            newAnswer.node.setPosition(0 ,-i * (currentAnswerHeight + 20));
            //增加父节点的高度
            this.answerListNode.height += (currentAnswerHeight + 20);

            //绑定点击事件
            newAnswer.node.on(cc.Node.EventType.TOUCH_START,function(event){ //触摸开始 缩小选项
                //event是触摸事件的回调参数，event.target可以获取当前你触摸了谁
                event.target.runAction(cc.scaleTo(0.1, 0.8, 0.8));
            },newAnswer.node);
            //同上
            newAnswer.node.on(cc.Node.EventType.TOUCH_CANCEL,function(event){//触摸取消 还原选项
                event.target.runAction(cc.scaleTo(0.1, 1 , 1));
            },newAnswer.node);
            newAnswer.node.on(cc.Node.EventType.TOUCH_END,function(event){//触摸结束 还原选项
                event.target.runAction(cc.scaleTo(0.1, 1 , 1));

                //打印log，event.target.getComponent(cc.Label) 可以获取到触摸节点的 label 组件 也就是上面的[newAnswer]对象
                var answerData = event.target.getComponent(cc.Label).answerData;
                cc.log("你选择了：" + event.target.getComponent(cc.Label).answerData);
                //判断是否结束
                if(answerData.isOver === '1'){
                    self.questionTitle.string = answerData.overContent;
                    self.answerListNode.removeAllChildren();
                    self.restartBtn.node.runAction(cc.fadeIn(0.2));
                }
                else{
                    //根据选择的答案 获取到下一个问题
                    self.loadQAContent(answerData.nextQuestion);
                }
            },newAnswer.node);
        }
    },
    hideQA: function(){
        this.questionTitle.node.runAction(cc.scaleTo(0.5, 0 , 0));
        var answerList = this.answerListNode.children;

        for(let answer in answerList){
            answerList[answer].runAction(cc.scaleTo(0.5, 0 , 0));
        }
    },
    showQA: function(){
        this.questionTitle.node.runAction(cc.scaleTo(0.5, 1 , 1));
        var answerList = this.answerListNode.children;

        for(let answer in answerList){
            answerList[answer].runAction(cc.scaleTo(0.5, 1 , 1));
        }
    },
    restartGame: function(){
        this.loadQAContent(1);
        this.answerListNodeDefaultHeight = this.answerListNode.height;
        this.restartBtn.node.runAction(cc.fadeOut(0.1));
    }
});